#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2e5 + 5;
const int M = 1e9 + 7;

struct Edge
{
    int from, to, w;
    bool operator<(const Edge &E)
    {
        return w < E.w;
    }
} e[N];

int n, m, a, b, c;
int f[N];

int find(int x)
{
    return f[x] == x ? x : f[x] = find(f[x]);
}

void merge(int x, int y)
{
    f[find(x)] = find(y);
}


ll mst()
{
    sort(e + 1, e + 1 + m);
    for (int i = 1; i <= n; i++)
        f[i] = i;

    ll res = 0;
    for (int i = 1; i <= m; i++) {
        if (e[i].w <= 0) {
            res += e[i].w;
            merge(e[i].from, e[i].to);
        } else {
            int x = find(e[i].from), y = find(e[i].to);
            if (x != y) {
                merge(x, y);
                res += e[i].w;
            }
        }
    }

    return res;
}
int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false), cin.tie(0);
    ll sum = 0;
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        cin >> a >> b >> c;
        e[i] = {a, b, c};
        sum += c;
    }

    ll res = sum - mst();

    cout << res << endl;
    return 0;
}